import React from "react";
import PubSub from "pubsub-js";

class Hello extends React.Component {
  state = {
    name: "张三",
  };
  // 订阅消息的代码写哪儿？componentDidMount
  componentDidMount() {
    this.xxx = PubSub.subscribe("aaa", (msg, payload) => {
      console.log("Hello 收到 aaa 消息", payload);

      this.setState({
        name: payload,
      });
    });
  }

  componentWillUnmount() {
    // 取消订阅
    PubSub.unsubscribe(this.xxx);
  }

  render() {
    return (
      <div>
        <h1>Hello - {this.state.name}</h1>
      </div>
    );
  }
}

export default Hello;
